package cn.zcscloud.mall.controller;

import cn.zcscloud.mall.common.PageRequest;
import cn.zcscloud.mall.common.R;
import cn.zcscloud.mall.mbg.model.SmsFlashPromotion;
import cn.zcscloud.mall.mbg.model.SmsFlashPromotionProductRelation;
import cn.zcscloud.mall.service.SmsFlashPromotionProductRelationService;
import cn.zcscloud.mall.vo.SmsFlashPromotionProductVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//商品关联
@CrossOrigin
@Slf4j
@RestController
@RequestMapping("/flashProductRelation")
public class SmsFlashPromotionProductRelationController {
    @Autowired
    private SmsFlashPromotionProductRelationService smsFlashPromotionProductRelationService;

    //批量选择商品添加关联
    @PostMapping("/create")
    public R create(@RequestBody List<SmsFlashPromotionProductRelation> relationList){
        log.info("relationList === {}",relationList);
        boolean b = smsFlashPromotionProductRelationService.saveBatch(relationList);
        if (b){
            return R.success("");
        }
        return R.error("添加失败",403);
    }

    //删除关联表
    @PostMapping("/delete/{id}")
    public R delect(@PathVariable Long id){
        boolean b = smsFlashPromotionProductRelationService.removeById(id);
        if (b){
            return R.success("");
        }
        return R.error("移除关联失败，可能不存在",403);
    }

    //分页查询不同场次关联及商品信息
    @GetMapping("/list")
    public R list(@RequestParam(value = "flashPromotionId") Long flashPromotionId,
                  @RequestParam(value = "flashPromotionSessionId") Long flashPromotionSessionId,
                  @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                  @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum){
        Page<SmsFlashPromotionProductVo> page = new Page<>(pageNum,pageSize);
        IPage<SmsFlashPromotionProductVo> iPage = smsFlashPromotionProductRelationService.getList(page,flashPromotionId,flashPromotionSessionId);
        return R.success(PageRequest.init(iPage));
    }

    /**
     * 修改关联信息
     * @param id id
     * @param relation 修改信息
     * @return R
     */
    @PostMapping("/update/{id}")
    public R update(@PathVariable Long id, @RequestBody SmsFlashPromotionProductRelation relation){
        boolean b = smsFlashPromotionProductRelationService.updateById(relation);
        if (b){
            return R.success("");
        }
        return R.error("操作失败",500);
    }

    /**
     * 获取管理商品促销信息
     * @param id 商品id
     * @return
     */
    @GetMapping("/{id}")
    public R getItem(@PathVariable Long id){
        SmsFlashPromotionProductRelation relation = smsFlashPromotionProductRelationService.getById(id);

        return R.success(relation);
    }

}
